package cn.itcast.web;

import cn.itcast.service.UserService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/search")
public class SearchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进行处理
        request.setCharacterEncoding("utf-8");
        //解析请求参数  获取关键字
        String keyword = request.getParameter("keyword");

        //关键字传递给service
        UserService service = new UserService();

        List<String> list = service.search(keyword);

        // 把集合数据转换成json
        String result = JSON.toJSONString(list);
        //设置响应体的类型
        response.setContentType("application/json;charset=utf-8");

        //结果设置响应体
        response.getWriter().write(result);




    }
}
